﻿using System;
using System.Collections.Generic;
using System.Linq;
using blackhouse.IdentityCms.Data;

namespace blackhouse.IdentityCms.Business.Templates {

    public class PageTemplates : ICollection<IPageTemplate> {
       
        #region Fields

        private readonly List<IPageTemplate> innerList;

        #endregion

        #region Properties

        public int Count {
            get { return this.innerList.Count; }
        }

        public bool IsReadOnly {
            get { return true; }
        }

        #endregion

        #region Constructors

        public PageTemplates()
        {
            ITemplateDbContext dbContext = DbFactory.GetTemplateDbContext();
            TemplateData[] templates = dbContext.GetTemplates();
            this.innerList = new List<IPageTemplate>(templates.Length);
            this.innerList.AddRange(templates.Select(td=>new CmsPageTemplate(td)));
        }

        #endregion

        #region Methods

        public void Add(IPageTemplate item) {
            throw new InvalidOperationException("Insert element to read only collection is not allowed");
        }

        public void Clear() {
            throw new InvalidOperationException("Clear read only collection is not allowed");
        }

        public bool Contains(IPageTemplate item) {
            return this.innerList.Contains(item);
        }

        public void CopyTo(IPageTemplate[] array, int arrayIndex) {
            this.innerList.CopyTo(array, arrayIndex);
        }

        public bool Remove(IPageTemplate item) {
            throw new InvalidOperationException("Remove element from read only collection is not allowed");
        }

        public IEnumerator<IPageTemplate> GetEnumerator() {
            return this.innerList.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return this.innerList.GetEnumerator();
        }

        #endregion

    }
}
